package com.actimize.webapp.action;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import com.actimize.model.Payee;
import com.actimize.model.Transaction;
import com.actimize.service.PayeeManager;
import com.opensymphony.xwork2.Preparable;

public class PayeeAction extends BaseAction implements Preparable  {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 32321L;

	private PayeeManager payeeManager;

	public void setPayeeManager(PayeeManager payeeManager) {
		this.payeeManager = payeeManager;
	}

	public String getGeneralInfo() throws IOException {
//		HttpServletRequest request = getRequest();
		return SUCCESS;
	}
	
	public String setNewPayee() throws IOException{
		
		return SUCCESS;
	}
	
	public String setNewBusinessPayee() throws IOException{
		
		return SUCCESS;
	}
	
	public String saveNewPayee() throws IOException{
		HttpServletRequest request = getRequest();
		Random rnd = new Random();
		Payee payee = new Payee();
		payee.setPayeeId(rnd.nextLong());
		payee.setPayeeName(request.getParameter("pnlSetupNewPayeePayaPerson:frmPayaPerson:stringBenName"));
		payee.setPayeeAccount(request.getParameter("pnlSetupNewPayeePayaPerson:frmPayaPerson:stringBenAccountNumber"));
		payee.setPayeeSort(request.getParameter("pnlSetupNewPayeePayaPerson:frmPayaPerson:stringBenSortCode")+"-"+
				request.getParameter("pnlSetupNewPayeePayaPerson:frmPayaPerson:stringBenSortCode_p2") +"-"+
				request.getParameter("pnlSetupNewPayeePayaPerson:frmPayaPerson:stringBenSortCode_p3")
				);
		payeeManager.savePayee(payee);
		Double amount = new Double(request.getParameter("pnlSetupNewPayeePayaPerson:frmPayaPerson:bigdecimalPayAmount"));
		accountManager.updateAmount("16470560", -amount);
		
		Transaction tran = new Transaction();
		
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		Date date = new Date();
		tran.setTranDate(dateFormat.format(date));
		
		tran.setTranDesc("Payment to "+payee.getPayeeName()+"("+payee.getPayeeSort()+", "+payee.getPayeeAccount()+")");
		tran.setTranOut(amount);
		tran.setTranType("DBT");
		tran.setTranIn(0.0);
		
		transactionManagerTmp.saveTran(tran);

		return SUCCESS;
	}
	
	public void prepare() throws Exception {
		
			
	}

}
